//https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/
//相交链表
//给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点，返回 null 。

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode* curA = headA;
    struct ListNode* curB = headB;
    while(curA && curB)
    {
        curA = curA->next;
        curB = curB->next;
    }
    struct ListNode* curL = curA;
    if(curA == NULL)
    curL = curB;

    int k = 0;
    while(curL)
    {
        k++;
        curL = curL->next;
    }

    if(curB == NULL)
    {
        while(k--)
        headA = headA->next;
    }
    else
    {
        while(k--)
        headB = headB->next;
    }

    while(headA && headB)
    {
        if(headA == headB)
        return headA;
        headA = headA->next;
        headB = headB->next;
    }
    return NULL;
}